{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bc488d99",
   "metadata": {},
   "source": [
    "# 本周任务\n",
    "## 优化ASR（自动语音识别）机器人\n",
    "## 连接图灵机器人（不用自己的字典）\n",
    "## 语音合成"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c6bf365",
   "metadata": {},
   "source": [
    "# 优化ASR（自动语音识别）机器人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5b89c4b3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-24T11:24:04.157258Z",
     "start_time": "2022-05-24T11:24:04.145435Z"
    }
   },
   "outputs": [],
   "source": [
    "# 准备百度key和secret\n",
    "API_KEY = 'LxG7I4xdAFLmsX43wKggBbFD'\n",
    "SECRET_KEY = 'WoT77EWlrhWC41m8YUrvn47WydEXK5IG'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b0c70a72",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-24T11:24:05.555704Z",
     "start_time": "2022-05-24T11:24:04.723138Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.7896871a699808b18565d1e2e3446872.2592000.1655983445.282335-26255993'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from baidu_speech import fetch_token,asr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1d898260",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-24T11:25:30.062906Z",
     "start_time": "2022-05-24T11:25:30.027227Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.06c8b042785aef58e7b41d5ddc512738.2592000.1655983529.282335-26255993'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "jiang_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "jiang_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75bb798d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "# 通过电脑的麦克风进行音频的获取\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1d9d2269",
   "metadata": {},
   "outputs": [],
   "source": [
    "asr_result = eval(jiang_token,'1.wav')['result'][0]\n",
    "asr_result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d355cffb",
   "metadata": {},
   "source": [
    "# 连接图灵测试机器人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a26cd0c9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-24T12:32:07.039999Z",
     "start_time": "2022-05-24T12:32:07.026610Z"
    }
   },
   "outputs": [],
   "source": [
    "TL_key=\"d5d5bb66f66d4da8bedb378df7adfa70\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0ed386dd",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-24T12:32:08.755279Z",
     "start_time": "2022-05-24T12:32:08.750293Z"
    }
   },
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "90b54d32",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-24T12:32:09.614983Z",
     "start_time": "2022-05-24T12:32:09.601550Z"
    }
   },
   "outputs": [],
   "source": [
    "# json.dumps 将 python中的dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "da653bb5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-24T12:32:10.477658Z",
     "start_time": "2022-05-24T12:32:10.462785Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'> {'reqType': 0, 'perception': {'inputText': {'text': '火龙果用英语怎么说'}}, 'userInfo': {'apiKey': 'd5d5bb66f66d4da8bedb378df7adfa70', 'userId': '000001'}}\n",
      "<class 'str'> {\"reqType\": 0, \"perception\": {\"inputText\": {\"text\": \"\\u706b\\u9f99\\u679c\\u7528\\u82f1\\u8bed\\u600e\\u4e48\\u8bf4\"}}, \"userInfo\": {\"apiKey\": \"d5d5bb66f66d4da8bedb378df7adfa70\", \"userId\": \"000001\"}}\n"
     ]
    }
   ],
   "source": [
    "TL_url = 'http://openapi.turingapi.com/openapi/api/v2'\n",
    "\n",
    "params = {   \n",
    "    \"reqType\":0,\n",
    "    \"perception\": {\n",
    "        \"inputText\": {\n",
    "            \"text\": \"火龙果用英语怎么说\"\n",
    "        },\n",
    "    },\n",
    "    \"userInfo\": {\n",
    "        \"apiKey\": TL_key,\n",
    "        \"userId\": \"000001\"\n",
    "    }\n",
    "}\n",
    "\n",
    "print(type(params),params)\n",
    "print(type(json.dumps(params)),json.dumps(params))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "65ed7704",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-24T12:32:14.252386Z",
     "start_time": "2022-05-24T12:32:14.151636Z"
    }
   },
   "outputs": [],
   "source": [
    "res = requests.post(TL_url,data = params)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f4a164e",
   "metadata": {},
   "source": [
    "# 语音合成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "45bbd0f7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-24T12:44:54.748853Z",
     "start_time": "2022-05-24T12:44:54.729062Z"
    }
   },
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "cannot import name 'tts' from 'baidu_speech' (C:\\Users\\86137\\Desktop\\API-2022-02\\API\\week13\\baidu_speech.py)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-17-b49072f419e3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mbaidu_speech\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mtts\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mImportError\u001b[0m: cannot import name 'tts' from 'baidu_speech' (C:\\Users\\86137\\Desktop\\API-2022-02\\API\\week13\\baidu_speech.py)"
     ]
    }
   ],
   "source": [
    "from baidu_speech import tts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "ff4cfffb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-24T12:44:55.232075Z",
     "start_time": "2022-05-24T12:44:55.215874Z"
    }
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'tts' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-18-8f92d8899ba9>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtts\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mjiang_token\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"这里是百度语音合成\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'tts' is not defined"
     ]
    }
   ],
   "source": [
    "tts(jiang_token,\"这里是百度语音合成\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "ab2107c6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-24T12:44:55.699430Z",
     "start_time": "2022-05-24T12:44:55.675926Z"
    }
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'AudioSegment' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-19-0616e1959cbf>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0msong\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mAudioSegment\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfrom_wav\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'result.wav'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mplay\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msong\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'AudioSegment' is not defined"
     ]
    }
   ],
   "source": [
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e002711f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-24T12:48:02.138000Z",
     "start_time": "2022-05-24T12:48:02.134012Z"
    }
   },
   "source": [
    "# 自动对话机器人（语音实现）\n",
    "\n",
    "> 1.我们讲话，录制音频，存放在‘1.wav’测试文件中\n",
    "> 2.语音识别：百度asr接口实现语音识别功能，将音频转化为文本\n",
    "> 3.接入图灵机器人，实现文本回复\n",
    "> 4.语音合成：百度tts接口实现语音合成功能，将回复的文本转换成音频\n",
    "> 5.通过pytho play模块进行播放"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "59401b86",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-24T13:47:23.408953Z",
     "start_time": "2022-05-24T13:47:23.336097Z"
    }
   },
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "cannot import name 'tts' from 'baidu_speech' (C:\\Users\\86137\\Desktop\\API-2022-02\\API\\week13\\baidu_speech.py)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-24-fbfcbd6a28c1>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     17\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     18\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mspeech_recognition\u001b[0m \u001b[1;31m#录制音频模块\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 19\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mbaidu_speech\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mfetch_token\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0masr\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mtts\u001b[0m \u001b[1;31m#百度语音系统模块\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     20\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mtuling\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mTuLing\u001b[0m \u001b[1;31m#图灵机器人\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     21\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mpydub\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mAudioSegment\u001b[0m \u001b[1;31m#播放音频模块\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mImportError\u001b[0m: cannot import name 'tts' from 'baidu_speech' (C:\\Users\\86137\\Desktop\\API-2022-02\\API\\week13\\baidu_speech.py)"
     ]
    }
   ],
   "source": [
    "\n",
    "# import speech_recognition # 录制音频模块\n",
    "# from baidu_speech import fetch_token,asr,tts  # 百度语音系统模块\n",
    "# from tuling import TuLing  # 图灵机器人\n",
    "# from pydub import AudioSegment  # 播放音频模块\n",
    "# from pydub.playback import play # 播放音频模块\n",
    "\n",
    "\n",
    "# r = speech_recognition.Recognizer()\n",
    "# # 通过电脑的麦克风进行音频的获取\n",
    "# with speech_recognition.Microphone() as source:\n",
    "#     audio = r.listen(source)\n",
    "# # 将数据保存到wav文件中\n",
    "# with open(\"1.wav\", \"wb\") as f: \n",
    "#     f.write(audio.get_wav_data(convert_rate=16000))\n",
    "\n",
    "    \n",
    "    \n",
    "import speech_recognition #录制音频模块\n",
    "from baidu_speech import fetch_token,asr,tts #百度语音系统模块\n",
    "from tuling import TuLing #图灵机器人\n",
    "from pydub import AudioSegment #播放音频模块\n",
    "from pydub.playback import play #播放音频模块\n",
    "\n",
    "#1.我们讲话，录制音频,存放在'l.wav‘测试文件中\n",
    "r = speech_recognition.Recognizer()\n",
    "#通过调用电脑的麦克风进行音频的获取\n",
    "with speech_recognition.Microphone( ) as source:\n",
    "    audio = r.listen(source)\n",
    "\n",
    "#将数据保存到wav文件中13\n",
    "with open( \" 1.wav\" , \"wb\" ) as f:\n",
    "    f.write(audio.get_wav_data(convert_rate=16000 ))\n",
    "\n",
    "\n",
    "# 2.语音识别:百度asr接口实现语音识别功能，将音频转换未文本\n",
    "jiang_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "\n",
    "asr_result = eval(asr(jiang_token,'1.wav'))[ 'result'][0]\n",
    "\n",
    "#3.接入图灵机器人,实现文本回复\n",
    "TL_result = TuLing(TL_key,asr_result)\n",
    "print(TL_result)\n",
    "                      \n",
    "#4。语音合成:百度tts接口实现语音合成功能，将回复的文本转换成音频\n",
    "tts(jiang_token,TL_result)\n",
    "\n",
    "# 5．通过python play模块进行擂放\n",
    "song =AudioSegment.from_wav ( 'result.wav ')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c37a56f4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
